package com.dsr.service.impl;

import cn.hutool.core.date.DateTime;
import com.dsr.bean.CollectTopBean;
import com.dsr.consts.PageResult;
import com.dsr.consts.Result;
import com.dsr.consts.e.UserProperty;
import com.dsr.dao.SpitDao;
import com.dsr.entity.Collect;
import com.dsr.entity.Orders;
import com.dsr.entity.Spit;
import com.dsr.service.SpitService;
import com.dsr.util.BasicUtil;
import com.dsr.util.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;

/**
 * (Spit)表服务实现类
 *
 * @author dsr
 * @since 2020-09-20
 */
@Service("spitService")
public class SpitServiceImpl implements SpitService {
    @Autowired
    private SpitDao spitDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public Result getEntity(String Id) {
        Optional<Spit> entity = spitDao.findById(Id);
        return entity.map(Result::success).orElseGet(() -> Result.error("id错误"));
    }

    @Override
    public Result save(Spit spit) {
        spit.set_id(String.valueOf(idWorker.nextId()));
        spit.setNickname(BasicUtil.getToken(UserProperty.NICK_NAME));
        spit.setPublishTime(DateTime.now());
        spit.setPeopleIcon(BasicUtil.getToken(UserProperty.AVATAR_URl));
        spit.setUserId(BasicUtil.getToken(UserProperty.USER_ID));
        spit.setState(0);
        spit.setComment(0);
        spit.setThumbUp(0);
        spitDao.insert(spit);
        return Result.success("发布成功");
    }

    @Override
    public Result update(Spit spit) {
        spitDao.insert(spit);
        return Result.success("修改成功");
    }

    @Override
    public Result delete(String Id) {
        if (StringUtils.isNotEmpty(Id)) {
            spitDao.deleteById(Id);
            return Result.success("删除成功");
        }
        return Result.error("请选择要删除的行");
    }

    @Override
    public Result query(int page, int size, Spit spit, String type) {
        Pageable pageable = PageRequest.of(page-1, size);
        int end=0;
        int start=0;
        switch (type){
            case "all":end=6;start=-1;break;
            case "good":end=6;start=3;break;
            case "med":end=4;start=1;break;
            case "bad":end=2;start=-1; break;
            default:end=6;start=-1;
        }
        Page<Spit> spitPage=null;
        if("img".equals(type)){
            spitPage = spitDao.findAllByFlowerIdAndImgUrlStartsWith(spit.getFlowerId(),"http",pageable);
        }else {
            spitPage = spitDao.findAllByFlowerIdAndRateBetween(spit.getFlowerId() ,start,end,pageable);
        }
        spitPage.getContent().forEach(item->{
            if(StringUtils.isNotEmpty(item.getImgUrl())){
                item.getImgUrlList().addAll(Arrays.asList(item.getImgUrl().split(",")));
            }
        });
        return Result.success(new PageResult<Spit>(spitPage.getTotalElements(), spitPage.getContent()));
    }

    @Override
    public Result list() {
        return Result.success(spitDao.findAll());
    }

    @Override
    public Result commitSum() {
        ArrayList<Integer> integers = new ArrayList<>();
        Integer img = spitDao.countByImgUrlStartsWith("http");
        Integer good = spitDao.countByRateBetween(3, 6);
        Integer med = spitDao.countByRateBetween(1, 4);
        Integer bad = spitDao.countByRateBetween(-1, 2);
        integers.add(good);
        integers.add(med);
        integers.add(bad);
        integers.add(img);
        return Result.success(integers);
    }



}